package com.ag.score.thread.service;

import com.ag.common.tools.DateUtil;
import com.ag.score.content.ApplicationContextProvider;
import com.ag.score.entity.TRecord;
import com.ag.score.entity.TUser;
import com.ag.score.mapper.TConfigMapper;
import com.ag.score.mapper.TRecordMapper;
import com.ag.score.mapper.TUserMapper;
import com.ag.score.util.MinuteParam;
import com.alibaba.fastjson.JSONObject;
import com.redis.RedisKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class RecordService {

    protected final static Logger logger = LoggerFactory.getLogger(RecordService.class);
    protected TConfigMapper tConfigMapper = ApplicationContextProvider.getBean(TConfigMapper.class);
    protected TRecordMapper tRecordMapper = ApplicationContextProvider.getBean(TRecordMapper.class);
    protected TUserMapper tUserMapper = ApplicationContextProvider.getBean(TUserMapper.class);

    public void processRecord(){
        int hour = DateUtil.getHour();
        if(hour< MinuteParam.startHour || hour>=MinuteParam.endHour){
            return ;
        }
        int secend = DateUtil.getSecend();
        if(secend<15){
            return;
        }
        //遍历记录
        HashMap<String,Object> param = new  HashMap<String,Object>();
        param.put("status",0);
        param.put("recordtype",MinuteParam.recordType);
        param.put("endtime",DateUtil.date2String(new Date(),"HH:mm"));
        List<TRecord> recordList = tRecordMapper.queryPage(param);
        if(recordList.size() == 0){
            return;
        }
        HashMap<String,Double> dataParam = new HashMap<String,Double>();
        for(int i=0;i<recordList.size();i++){
            TRecord record = recordList.get(i);
            double startData = record.getStartdata();
            String endTime = record.getEndtime();
            Double endData = dataParam.get(endTime);
            if(endData == null){
                String val = RedisKit.getString(endTime);
                if(val!=null){
                    endData = new Double(val);
                }
            }
            if(endData == null || endData==0){
                logger.info("RecordService.processRecord enddata is null \r\n "+ JSONObject.toJSON(record).toString());
                endData = record.getStartdata();
            }
            dataParam.put(endTime,endData);
            int type = record.getType();
            record.setEnddata(endData);
            if(type == 1){
                if(startData<endData){
                    scoreWin(record);
                }else{
                    record.setStatus(1);
                    record.setResscore(0);
                    tRecordMapper.updateById(record);
                }
            }else if(type == -1){
                if(startData>endData){
                    scoreWin(record);
                }else{
                    record.setStatus(1);
                    record.setResscore(0);
                    tRecordMapper.updateById(record);
                }
            }
        }

    }
    private void scoreWin(TRecord record){

        double vinlv = record.getWinlv();
        double resScore = getFormatTwo( record.getUsescore() * (1+vinlv));
        record.setStatus(1);
        record.setResscore(resScore);
        tRecordMapper.updateById(record);
        TUser user = tUserMapper.selectById(record.getUid());
        if (user != null) {
            user.setScore(user.getScore() + record.getResscore() );
            tUserMapper.updateById(user);
        }
    }

    /**
     * 格式化double
     * @param dnum
     * @return
     */
    protected static  double  getFormatTwo(double  dnum){
        DecimalFormat df=new DecimalFormat(".##");
        return new Double(df.format(dnum));
    }


}
